<?php
namespace App\Logics;

use App\Models\Base\AdminRole;
use App\Models\Base\AdminRoleMenu;
use App\Utils\CacheUtils;
use core\Singleton;
use App\Models\AdminRoleModel;


class AdminRoleLogic{

    use Singleton;

    /**
     * 获取文章详情
     *
     * @param  int  $id
     * @return mixed
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/28 下午2:18
     */
    public function getDetail(int $id){
        if($id == 0){
            return ['menu_ids' => []];
        }
        $data = AdminRoleModel::getInstance()->getDetail($id);
        $menuIds = (new MenuLogic())->getMenuIdsByRoleId($id);
        $data['menu_ids'] = [];
        if(! empty($menuIds)){
            $data['menu_ids'] = $menuIds;
        }
        return $data;

    }

    public function getList(){
        return AdminRole::get(['id', 'role_name']);
    }

    /**
     * 获取分页列表
     *
     * @return mixed
     * @author lichunguang 153102250@qq.com
     * @since 2022/8/31 下午8:18
     */
    public function getPageList(int $page, int $pageSize){
         return AdminRoleModel::getInstance()->getPageList($page, $pageSize);
    }

    /**
     * 添加或者修改数据, 单条数据的修改
     * @param int $id
     * @param array $data
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/28 下午4:55
     */
    public function save(int $id, array $args){
        $menuList = $args['menu_list'];
        //print_r($id);exit;
        $data = [
            'role_name' => $args['role_name'] ?? '',
            'role_intro' => $args['role_intro'] ?? '',
        ];
        $dataList = [];
        if($id > 0){
            $current_ids = AdminRoleMenu::where('role_id', $id)->pluck('menu_id');
            //array_diff($a1, $a2) 返回在 $a1 中找到的元素，但不在 $a2中的元素
            if(!empty($current_ids)){
                $insert_ids = array_diff($menuList, $current_ids);
                $delete_ids = array_diff($current_ids, $menuList);
                if(! empty($delete_ids)) {
                    AdminRoleMenu::where('role_id', $id)->whereIn('menu_id', $delete_ids)->delete();
                }
            }else{
                $insert_ids = $menuList;
            }
            foreach ($insert_ids as $v){
                $dataList[] = [
                    'role_id' => $id,
                    'menu_id' => $v,
                    'created_at' => now(),
                    'updated_at' => now()
                ];
            }
            AdminRoleMenu::batchInsert($dataList);
            $result = AdminRoleModel::where('id', $id)->update($data);
            $cacheKey = 'permission_url_'.$id;
            CacheUtils::del($cacheKey);
            (new MenuLogic())->getPermissionUrlByRoleId($id);
            $cacheKey = 'permission_menu_'.$id;
            CacheUtils::del($cacheKey);
            (new MenuLogic())->getMenuByRoleId($id);
            return $result;
        }else{
            $id = AdminRoleModel::insert($data);
            foreach ($menuList as $v){
                $dataList[] = [
                    'role_id' => $id,
                    'menu_id' => $v,
                    'created_at' => now(),
                    'updated_at' => now()
                ];
            }
            AdminRoleMenu::batchInsert($dataList);
            (new MenuLogic())->getPermissionUrlByRoleId($id);
            return $id;
        }
    }

    /**
     * 单条记录删除
     *
     * @param  int  $id
     * @return mixed
     * @author lichunguang 153102250@qq.com
     * @since 2022/4/28 下午5:00
     */
    public function delete(int $id){
        //先删除角色的菜单关联
        AdminRoleMenu::where('role_id', $id)->delete();
        AdminRoleModel::getInstance()->where('id', $id)->delete();
    }

}